/**
 * 修改用户信息功能。
 * @author 黄广星
 */

package com.taxsystem.huangguangxing.login;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import com.main.taxsystem.R;
import com.taxsystem.tool.CommonOperations;
import com.taxsystem.tool.NetUtil;

import android.app.ActionBar;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AlterUserActivity extends Activity {
	
	private static final String ACTION = "com.taxsystem.huangguangxing.login.HTTP_POST_RESPONSE_USER";
	private String url = "http://itaxmanager.duapp.com/app/user/update";
	
	private LocalBroadcastManager lbm;
	private MyThread myThread;

	private Button alter;
	private EditText userId, userCode, userName, taxpayerCode, taxpayerName,
			swdjh;

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.alter_user_layout);
		
		ActionBar bar = getActionBar();
        bar.setHomeButtonEnabled(true);
        bar.setIcon(R.drawable.actionbar_icon);
        bar.setDisplayHomeAsUpEnabled(true);
        bar.setBackgroundDrawable(new ColorDrawable(0xff34a4f7));
		
		registerBroadcastReceiver();//注册
		
		alter = (Button) findViewById(R.id.alter_user);
		userId = (EditText) findViewById(R.id.user_id_alter);
		userCode = (EditText) findViewById(R.id.user_code_alter);
		userName = (EditText) findViewById(R.id.user_name_alter);
		taxpayerCode = (EditText) findViewById(R.id.taxpayer_code_alter);
		taxpayerName = (EditText) findViewById(R.id.taxpayer_name_alter);
		swdjh = (EditText) findViewById(R.id.swdjh_alter);

		if(LoginMassege.isLogin())
		{
			initSet(LoginMassege.getUser());
		}
		else
		{
			Toast.makeText(AlterUserActivity.this, "你还没登录",
					Toast.LENGTH_SHORT).show();
			Intent intent_login = new Intent();
			intent_login.setClass(AlterUserActivity.this, LoginActivity.class);
			startActivity(intent_login);
			finish();
		}
		
		alter.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				if (isOK()) {
					
					visitNetwork();
					
//					Toast.makeText(AlterUserActivity.this, "修改成功",
//							Toast.LENGTH_SHORT).show();
				}

			}
		});

	}
	
	void visitNetwork() {
		if (CommonOperations.isNetworkConnected(this)) {
			myThread = new MyThread();
			myThread.run();
		} else
			Log.w("tag1", "device can't connect network");
	}

	void registerBroadcastReceiver() {
		lbm = LocalBroadcastManager.getInstance(this);
		IntentFilter filter = new IntentFilter(ACTION);
		BroadcastReceiver myRecive = new MyRecive();
		lbm.registerReceiver(myRecive, filter);
	}
	
	private class MyThread extends Thread {
		@Override
		public void run() {

			Intent intent = new Intent(ACTION);
			try {
				
				 NameValuePair pair1 = new BasicNameValuePair("userId", userId.getText().toString());
		         NameValuePair pair2 = new BasicNameValuePair("userCode",userCode.getText().toString());
		         NameValuePair pair3 = new BasicNameValuePair("password",LoginMassege.getUser().getPassword());
		         NameValuePair pair4 = new BasicNameValuePair("userName",userName.getText().toString());
		         NameValuePair pair5 = new BasicNameValuePair("taxpayerCode", taxpayerCode.getText().toString());
		         NameValuePair pair6 = new BasicNameValuePair("taxpayerName", taxpayerName.getText().toString());
		         NameValuePair pair7 = new BasicNameValuePair("swdjh",swdjh.getText().toString());
		         
		         List<NameValuePair> pairList = new ArrayList<NameValuePair>();
		         pairList.add(pair1);
		         pairList.add(pair2);
		         pairList.add(pair3);
		         pairList.add(pair4);
		         pairList.add(pair5);
		         pairList.add(pair6);
		         pairList.add(pair7);
		         
				String result=NetUtil.getResultPost(url, pairList);
				intent.putExtra("JSON", result);
				lbm.sendBroadcast(intent);
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}
	}

	private class MyRecive extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {

			String jsonString = intent.getStringExtra("JSON");
			if (jsonString == null) {

				Log.e("tag1", "MyRecive.onReceive():jsonString is null");
				return;
			}

			try {
				
				JSONObject jsonObject = new JSONObject(jsonString);
				int isLogin = jsonObject.getInt("code");
				if(isLogin==1)
				{
					Toast.makeText(AlterUserActivity.this, "修改成功",
							Toast.LENGTH_SHORT).show();
					finish();
				}
				@SuppressWarnings("unused")
				String loginMessage = jsonObject.getString("message");
				// JSONObject objectUser = new JSONObject("data");//有问题
				JSONObject objectUser = new JSONObject(
						jsonObject.getString("data"));// 有问题
				User user = new User();

				user.setUserId(objectUser.getInt("userId"));
				user.setUserCode(objectUser.getString("userCode"));
				user.setUserName(objectUser.getString("userName"));
				user.setPassword(objectUser.getString("password"));
				user.setType(objectUser.getInt("type"));
				user.setState(objectUser.getInt("state"));
				user.setLoginTime(objectUser.getString("loginTime"));

			} catch (Exception e) {
				e.printStackTrace();
			} finally {

			}
		}
	}

	private void initSet(User user)
	{
		userId.setText(user.getUserId()+"");
		userCode.setText(user.getUserCode());
		userName.setText(user.getUserName());
		taxpayerCode.setText(user.getTaxpayerCode());
		taxpayerName.setText(user.getTaxpayerName());
		swdjh.setText(user.getSwdjh());
	}
	
	// 判断输入信息是否合法
	private boolean isOK() {
		boolean flag = false;
		if (userId.getText().toString().equals(""))
			Toast.makeText(AlterUserActivity.this, "用户ID不能为空", Toast.LENGTH_SHORT)
					.show();
		else if (userCode.getText().toString().equals(""))
			Toast.makeText(AlterUserActivity.this, "用户账号不能为空", Toast.LENGTH_SHORT)
					.show();
		else if (userName.getText().toString().equals(""))
			Toast.makeText(AlterUserActivity.this, "用户名称不能为空", Toast.LENGTH_SHORT)
					.show();
//		else if (taxpayerCode.getText().toString().equals(""))
//			Toast.makeText(AlterUserActivity.this, "纳税人编号不能为空", Toast.LENGTH_SHORT)
//					.show();
//		else if (taxpayerName.getText().toString().equals(""))
//			Toast.makeText(AlterUserActivity.this, "纳税人姓名不能为空", Toast.LENGTH_SHORT)
//					.show();
//		else if (swdjh.getText().toString().equals(""))
//			Toast.makeText(AlterUserActivity.this, "税务登记号不能为空", Toast.LENGTH_SHORT)
//					.show();
		else
			flag = true;
		return flag;
	}
}
